package xia.wan.reptile.util;

import cn.hutool.core.util.ReUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import xia.wan.reptile.entity.Chapter;
import xia.wan.reptile.entity.ComicPage;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: reptile
 * @interfaceName HttpComicPage
 * @description: 获取章节图片
 * @author: 太白
 * @create: 2022-12-14 18:56
 **/
public class HttpComicPage {


    public List<ComicPage> gainChapter(Chapter chapter){
//        每个章节的url
        String url = chapter.getUrl();
//        获取章节页面
        String cartoonOneHtml = HttpUtil.get(url);
        ArrayList<ComicPage> comicPages = new ArrayList<ComicPage>();
//        获取章节图片
        List<String> imgs = ReUtil.findAll("<script>;var siteName = \"\";var siteUrl = \"https://www.fengmh.com\";;var chapterImages =(.*?);var chapterPath = \"\";", cartoonOneHtml, 1);
        for (String img : imgs) {
            JSONArray array = JSONUtil.parseArray(img);
            for (int i = 0; i < array.size(); i++) {
//                System.out.println(array.get(i));
                ComicPage comicPage = new ComicPage();
                comicPage.setName(String.valueOf(i));
                comicPage.setUrl((String) array.get(i));
                comicPages.add(comicPage);
            }

        }

        return comicPages;
    }



}
